package com.apes.scm.settle.settle.respository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.settle.settle.model.TaxBillItem;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TaxBillItemRepository extends CustomRepository<TaxBillItem, String> {

    @Query("select b from TaxBillItem b join b.taxBill a " +
            "WHERE a.company.id = :companyId " +
            "and a.dept.id = :deptId " +
            "and a.partner.id = :partnerId " +
            "and a.cooperation.id = :cooperationId " +
            "and a.state = 'approve' " +
            "and b.amount - b.actualAmount - b.freezeAmount > 0")
    List<TaxBillItem> findTaxBillItemForPayment(@Param("companyId") String companyId, @Param("deptId") String deptId,
                                                @Param("partnerId") String partnerId, @Param("cooperationId") String cooperationId);

    @Query("select b from TaxBillItem b join b.taxBill a " +
            "WHERE a.company.id = :companyId " +
            "and a.partner.id = :partnerId " +
            "and a.cooperation.id = :cooperationId " +
            "and a.state = 'approve'" +
            "and ((b.amount - b.actualAmount - b.freezeAmount-b.expenseAmount <> 0) OR (b.amount - b.actualAmount - b.freezeAmount-b.expenseAmount <> 0 and b.type.code = 'red' ))")
    List<TaxBillItem> findTaxBillItemForPaymentForForm(@Param("companyId") String companyId,
                                                       @Param("partnerId") String partnerId, @Param("cooperationId") String cooperationId);

    /**
     * 查询需要付款的 税票明细
     *
     * @param companyId     公司编码
     * @param partnerId     客户
     * @param cooperationId 合作编码
     * @return 明细
     */
    @Query("select b from TaxBillItem b join b.taxBill a " +
            "WHERE a.company.id = :companyId " +
            "and a.partner.id = :partnerId " +
            "and a.cooperation.id = :cooperationId " +
            "and b.amount - b.actualAmount - b.freezeAmount <> 0" +
            "and a.state = 'approve' ")
    List<TaxBillItem> findTaxBillItemForPayment(@Param("companyId") String companyId,
                                                @Param("partnerId") String partnerId, @Param("cooperationId") String cooperationId);
}
